<?php
if (preg_match ( "/".basename ( __FILE__ )."/", $_SERVER ['PHP_SELF'] )) {
	die ("<h1>Incorrect access</h1>You cannot access this file directly.");
}
class report extends Form {
	function report(){
		$this->name  = 'reportForm';

		if(isset($_SESSION['userDeal'])){
			$code = FunctionLib::getParam('code');
			if($code == 'print'){
				$this->printTemplate();
			}else{
				$this->reportList();
			}
		}
	}
	function printTemplate(){
		global $display;
		
		echo $display->output('print',true,'report');
	}
	function reportList(){
		global $display;

		$user = User::getUser(User::id());
		$items = '';
		$itemArr =  array();
		if(!empty($user['item_id'])){
			$items =  implode("','", $user['item_id']);
			$itemArr = DB::fetch_all("SELECT id, title_seo FROM ".T_ITEMS." WHERE id IN ('$items')");
		}
		elseif($_SESSION['userDeal']['username'] != 'adminmc'){
			$this->setFormError('','Bạn chưa có quyền cho sản phẩm nào!');
		}
		$ticket = array();
		if($this->errNum == 0){
			$from_day = FunctionLib::getParamInt('from_day',1);
			$from_month = FunctionLib::getParamInt('from_mon',1);
			$from_year = FunctionLib::getParamInt('from_year',2011);
			
			$to_day = FunctionLib::getParamInt('to_day',1);
			$to_month = FunctionLib::getParamInt('to_mon',1);
			$to_year = FunctionLib::getParamInt('to_year',2011);
			
			$from = mktime(0,0,0,$from_month,$from_day,$from_year);
			$to = mktime(23,59,59,$to_month,$to_day,$to_year);
			
			$id = FunctionLib::getParamInt('item_id',0);
			if($id > 0){
				$items = $id;
			}
			
			if($from > TIME_NOW){
				$from = TIME_NOW;
			}
			if($to > TIME_NOW){
				$to = TIME_NOW;
			}
			if($from >= $to){
				$this->setFormError('','Thời gian không hợp lệ');
			}else{
				$search_value   = " WHERE status > 0 ";
				if($_SESSION['userDeal']['username'] != 'adminmc'){
					$search_value .= " AND item_id IN ('".$items."')";
					$search_value .= " AND approved_user = '".$_SESSION['userDeal']['username']."'";
				}
				$search_value .= " AND time_using >= $from AND time_using <= $to";
				
				$arrOrderID = '';
				$res = DB::query('SELECT time_using, phone, ticket_code, orderid FROM ' . T_TICKET . $search_value.' ORDER BY time_using DESC');
				while($v = @mysql_fetch_assoc($res)){
					$ticket[$v['orderid']] = $v;
					$arrOrderID .= $v['orderid'].',';
				}
				if($arrOrderID != ''){
					$arrOrderID = substr($arrOrderID,0,-1);
					$sql = "SELECT name_create,guest_create,id
							FROM ".T_ORDERS."
							WHERE id IN(".$arrOrderID.")";
					$res = DB::query($sql);
					while($row = @mysql_fetch_assoc($res)){
						$ticket[$row['id']]['guest_create'] = ($row['guest_create'] ? $row['guest_create'] : $row['name_create']);
					}
				}
				$display->add('ticket',$ticket);
			}
			
			$display->add('item_sel', $itemArr);
			$display->add('item_id', $id);
			$display->add('from_day', $from_day);
			$display->add('from_mon', $from_month);
			$display->add('from_year', $from_year);
			$display->add('to_day', $to_day);
			$display->add('to_mon', $to_month);
			$display->add('to_year', $to_year);
		}

		$display->add('msg',$this->showFormErrorMessages(1));
		$display->add('total',count($ticket));
		echo $display->output('report',true,'report');
	}
	
}
?>